<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>部门列表</title>
<%@ include file="/WEB-INF/jsp/kt/common/inc.jsp" %>
<script type="text/javascript" src="${_resources}JSON-js-master/json2.js"></script>
<link href='${_resources}css/style.css' type='text/css' rel='stylesheet' />
<script type="text/javascript">
var menuList;
var customer = "${numberBuilder}";
$(function (){
	//初始化表格
    if( customer == 'tianjin712suo'){
        menuList = $("#menuList").bootstrapTable({
            url : '${_baseUrl}/departmentController/select',
            pagination: true,
            pageList: [5, 10, 20, 50],
            queryParams: queryParams,
            columns: [{
                field: 'checked',
                checkbox: true,
                width: 35,
                cellStyle : function(value, row, index) {
                    if(row.status =='1'){
                        return {css:{'color':'#AAAAAA'}};
                    }else{
                        return {css:{'color':''}};
                    }
                } ,
                formatter : function(value, row, index) {
                    if(row.status =='1'){
                        return {
                            disabled: true
                        };
                    }else {
                        return;
                    }
                }
            }, {
                field: 'id',
                title: 'id',
                visible:false,
                width: 5
            },{
                field: 'deptName',
                title: '部门名称',
                width: 65,
                formatter : function(value, row, index) {
                    var str="";
                    if(row.status =='1'){
                        str += value;
                    }else {
                        str +="<a onClick=\"editFun('"+row.id+"');\">"+value+"</a>";
                    }
                    return str;
                }
            },{
                field: 'parentName',
                title: '上级部门',
                width: 65
            },{
                field: 'deptNumber',
                title: '部门编号',
                width: 65
            },{
                field: 'userCount',
                title: '部门<br/>人数',
                width: 60
            },{
                field: 'deptCreateTime',
                title: '部门创建时间 ',
                width: 150
            },{
                title: '操作',
                width: 100,
                formatter: function(value, row, index){
                    if(row.status == '1'){
                        return "<shiro:hasPermission name='departTianSevenController:enable'> <a href='javascript:void(0)' onClick=\"updateStatus('"+row.id+"', '0','"+row.deptName+"');\">还原</a> </shiro:hasPermission>"
                        +"<shiro:lacksPermission name='departTianSevenController:enable'> -- </shiro:lacksPermission>"

                    } else {
                        return "<shiro:hasPermission name='departTianSevenController:disable'> <a href='javascript:void(0)' onClick=\"updateStatus('"+row.id+"', '1','"+row.deptName+"');\">废弃</a> </shiro:hasPermission>"
                            +"<shiro:lacksPermission name='departTianSevenController:disable'> -- </shiro:lacksPermission>"
                    }

                }
            }]
        });
    }else {
        menuList = $("#menuList").bootstrapTable({
            url : '${_baseUrl}/departmentController/select',
            pagination: true,
            pageList: [5, 10, 20, 50],
            queryParams: queryParams,
            columns: [{
                field: 'checked',
                checkbox: true,
                width: 35
            }, {
                field: 'id',
                title: 'id',
                visible:false,
                width: 5
            },{
                field: 'deptName',
                title: '部门名称',
                width: 65,
                formatter : function(value, row, index) {
                    var str="";
                    str +="<a onClick=\"editFun('"+row.id+"');\">"+value+"</a>";
                    return str;
                }
            },{
                field: 'parentName',
                title: '上级部门',
                width: 65
            },{
                field: 'deptNumber',
                title: '部门编号',
                width: 65
            },{
                field: 'userCount',
                title: '部门<br/>人数',
                width: 60
            },{
                field: 'deptCreateTime',
                title: '部门创建时间 ',
                width: 150
            }]
        });
    }

	
	function queryParams(params){
		return {
		    pageSize: params.limit,
		    pageNo: params.pn,
		    sort : params.sort,
		    order : params.order,
		    deptName:$("#departName").val(),
            parentId : $("#parentName option:selected").val() == '' ? null : $("#parentName option:selected").val()
	   }
	}
	
	$("#departName").keydown(function(event) {//给输入框绑定按键事件
	    if(event.keyCode == "13") {//判断如果按下的是回车键则执行下面的代码
	    	refreshTable();
	  	}
	})
	
})

function deleteFun(){
	 var rows= $("#menuList").bootstrapTable('getSelections');
		if(rows.length==0){
			layer.msg("请先选择需要删除的部门");
		}else{
			var rowsString = JSON.stringify(rows); 
			var path="${_baseUrl}/departmentController/delete";
			 $.ajax({
					type: "POST",    
					async: false,    
					url:path,   
					data:{
						"rows":rowsString,
					},    
					dataType:"text",    
					success: function (data) {   
						layer.msg("删除成功");
						// menuList.bootstrapTable('refresh');
                        window.location.reload();//刷新正个页面
					}   
				});
		}
}

function updateStatus(id, status, deptName) {
    var path="${_baseUrl}/departTianSevenController/updateStatus";
    $.ajax({
        type: "POST",
        async: false,
        url:path,
        data:{
            "id":id,
            "status":status
        },
        dataType:"text",
        success: function (data) {
            layer.msg("操作成功");
            // menuList.bootstrapTable('refresh');
            window.location.reload();//刷新正个页面
        }
    });

}

function synchronization(){
	 var rows= $("#menuList").bootstrapTable('getSelections');
		if(rows.length==0){
			layer.msg("请先选择需要同步的部门");
		}else{
			var ids = new Array();
		    for(var i=0;i<rows.length;i++){
		    	ids.push(rows[i].id);
		    }
			var path="${_baseUrl}/kpProjectController/synchronizationNode";
			 $.ajax({    
					type: "POST",    
					async: false,    
					url:path,   
					data:{
						"ids":ids.join(","),
					},    
					dataType:"json",    
					success: function (data) {
						if(data.code == "1"){
							layer.msg("同步成功");
							menuList.bootstrapTable('refresh');	
						}else{
							layer.msg(data.message);
							menuList.bootstrapTable('refresh');	
						}
						
					}   
				}); 
		}
}
function addFun(){
	// 判断除了三员之外是否有其他用户
	var path="${_baseUrl}/userController/selectNoDeptUserCountExceptAdmin";
	var flag = false;
	$.ajax({    
		type: "POST",    
		async: false,    
		url:path,   
		data:{
		},    
		dataType:"json",    
		success: function (data) {  
			if(data.code == "1"){
				var rsData =  JSON.parse(data.data);
				if(rsData.count != undefined && rsData.count == 0){
					flag = true;
				}
			}
			
			if(flag){
				layer.msg("系统中没有可以设置部门的用户！"); // 三员和超管不需要设置部门
				return false;
			}else{
				layerIndex =layer.open({
			        type: 2 //Page层类型
			        ,area: ['730px', '445px']
			        ,title: '添加部门'
			        ,shade: 0.3 //遮罩透明度
			        ,maxmin: true //允许全屏最小化
			        ,anim: -1 //0-6的动画形式，-1不开启
			        ,content: "${_baseUrl}/departmentController/addPage?random="+Math.random()
			    });
			 	layer.full(layerIndex);
			}
		}   
	}); 
	
	
	
	
	
}
function editFun(id){  
	layerIndex =layer.open({
        type: 2 //Page层类型
        ,area: ['700px', '430px']
        ,title: '修改部门信息'
        ,shade: 0.3 //遮罩透明度
        ,maxmin: true //允许全屏最小化
        ,anim: -1 //0-6的动画形式，-1不开启
        ,content: "${_baseUrl}/departmentController/editPage?id="+id+"&random="+Math.random()
    });
 	layer.full(layerIndex);
}
/* 关闭弹出框 */
function closeWin(){
	//$("#popDiv").dialog('close');
	layer.closeAll();
}

function refreshTable(){
	menuList.bootstrapTable('refresh');
}
</script>
<style type="text/css">
.func_search_div{width: 250px;}
.func_search input{width: 215px;}
</style>
</head>
<body>
<div class="submenu">
    <div  class="func_header">
        <span title="部门管理">当前位置 ： <a href="javascript:;"></a>部门管理</span>
    </div>
    <div class="func_bar">
        <div class="search_container">
		    <div class="func_search">
		    	<div class="func_search_div"></div>
	            <input type="text" name="departName" id="departName" value=""  placeholder="请输入关键词" />
	            <a class="search_btn" id="search" href="javascript:;" onclick="refreshTable()"></a>
	        </div>
            <c:if test="${numberBuilder == 'tianjin712suo'}">
                <select style="float:left;width:110px;margin-left: 15px;margin-bottom: 6px;" id="parentName" 

onchange="refreshTable()" class="func_select">
                    <option value=''>上级部门</option>
                    <c:forEach var="parentNameList" items="${parentNameList}">
                        <option value='${parentNameList.parentId}'>${parentNameList.parentName}</option>
                    </c:forEach>
                </select>
            </c:if>
        </div>
        <div class="btn_container">
         <c:if test="${numberBuilder == 'tianjin712suo'}">
            <shiro:hasPermission name="kpProjectController:synchronizationNode">
            	<a class="blue_btn" id="synchronization" href="javascript:;" onClick="synchronization();">部门信息同步

</a>
             </shiro:hasPermission>
         </c:if>
        	<shiro:hasPermission name="departmentController:addPage">
            	<a class="blue_btn" id="addDepartment" href="javascript:;" onClick="addFun();">添加部门</a>
            </shiro:hasPermission>

            <c:if test="${numberBuilder != 'tianjin712suo'}">
                <shiro:hasPermission name="departmentController:delete">
                    <a href="javascript:;" class="current_2" onClick="deleteFun();">删除部门</a>
                </shiro:hasPermission>

                <shiro:hasPermission name="departTianSevenController:disable"><a href="javascript:void(0)"  onClick="updateStatus('0', '0');">废弃</a></shiro:hasPermission>
            </c:if>
        </div>
    </div>
    <div style="overflow: auto; width:98%;margin: auto;">
	    <table id="menuList" class="table table-bordered table_inline"></table>
	</div>
</div>
</body>
</html>